#include <boost/asio.hpp>
#include <iostream>

using namespace boost::asio;
using namespace boost::system;

constexpr short port = 30001;

void recv(ip::udp::socket &socket_, std::array<char, 1024> &data,
          ip::udp::endpoint &sender_endpoint_) {

  socket_.async_receive_from(
      buffer(data), sender_endpoint_,
      [&](boost::system::error_code ec, std::size_t len) {
        if (!ec) {
          std::cout.write(data.data(), len);
          std::cout << std::endl;
          recv(socket_, data, sender_endpoint_);
        }
      });
}

int main() {
  io_context context;
  std::array<char, 1024> data;

  auto listen_addr = ip::make_address("0.0.0.0");
  auto multicast_addr = ip::make_address("239.255.0.1");

  ip::udp::socket socket_(context);
  ip::udp::endpoint listen_endpoint(listen_addr, port);
  ip::udp::endpoint sender_endpoint_;

  socket_.open(listen_endpoint.protocol());
  socket_.set_option(ip::udp::socket::reuse_address(true));
  socket_.bind(listen_endpoint);
  socket_.set_option(ip::multicast::join_group(multicast_addr));
  recv(socket_, data, sender_endpoint_);

  context.run();

  return 0;
}